package com.wuye.services.impl;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sdicons.json.validator.impl.predicates.Int;
import com.wuye.common.dao.Dao;
import com.wuye.common.vo.ComCompanyInfo;
import com.wuye.common.vo.PageInfo;
import com.wuye.common.vo.ZTreeInfo;
import com.wuye.constants.BaseConstants;
import com.wuye.dao.OrgDao;
import com.wuye.entity.BaseEntity;
import com.wuye.entity.Organization;
import com.wuye.mapper.CommonMapper;
import com.wuye.services.BranchServiceManager;
import com.wuye.util.MyBatisSessionFactory;

@Service("branchServiceManager")
public  class BranchServiceManagerImpl implements BranchServiceManager
{
	@Autowired
	private OrgDao orgDao;

	public void save(BaseEntity baseEntity) 
	{
		
		
	}

	public void setDao(Dao dao) {
		// TODO Auto-generated method stub
		
	}

	public List getObjects(Class clazz) {
		// TODO Auto-generated method stub
		return null;
	}

	public Object getObject(Class clazz, Serializable id) {
		// TODO Auto-generated method stub
		return null;
	}

	public void saveObject(Object o) 
	{
		// TODO Auto-generated method stub
		
	}

	public void removeObject(Class clazz, Serializable id) 
	{
		// TODO Auto-generated method stub
		
	}

	public void updateObjectInvalid(Object o) {
		// TODO Auto-generated method stub
		
	}

	public PageInfo getBranchRoots(int userId, String type, String keywords,int curPage) 
	{
		List<ComCompanyInfo> rtnList = null;
		PageInfo pageInfo = null;
		
		Map<String, Object> inMap = new HashMap<String, Object>();
		inMap.put("type", type);
		inMap.put("keywords", keywords);
		inMap.put("userId", userId);
		
		int offset = (curPage-1) * BaseConstants.BRANCH_PAGE_SIZE;
		inMap.put("offset", offset);
		inMap.put("limit", BaseConstants.BRANCH_PAGE_SIZE);
		
		int totalRecs = 0;
		int totalPageCount = 0;
		
		SqlSession session = null;
		
		try 
		{
			session = MyBatisSessionFactory.getSession();			
			CommonMapper commonMapper = session.getMapper(CommonMapper.class);
			totalRecs = commonMapper.getBranchRootsCnt(inMap);
			
			if (totalRecs > 0)
			{
				rtnList = commonMapper.getBranchRoots(inMap);
				totalPageCount = totalRecs / BaseConstants.BRANCH_PAGE_SIZE;						
				totalPageCount = (totalRecs % BaseConstants.BRANCH_PAGE_SIZE) > 0 ? (totalPageCount + 1) : totalPageCount;
				
				pageInfo = new PageInfo();
				pageInfo.setCurrentPage(curPage);
				pageInfo.setPerPageCount(BaseConstants.BRANCH_PAGE_SIZE);
				pageInfo.setTotalCount(totalRecs);
				pageInfo.setTotalPageCount(totalPageCount);
				pageInfo.setDataList(rtnList);
				
			}
			
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		
		return pageInfo;
	}
	
	public String getChildren(int rootId)
	{
		Map<String, Object> inMap = new HashMap<String, Object>();
		inMap.put("rootId", rootId);
		inMap.put("rtnStr", "");
		
		String rtnString = null;		
		SqlSession session = null;
		
		try 
		{
			session = MyBatisSessionFactory.getSession();			
			CommonMapper commonMapper = session.getMapper(CommonMapper.class);
			commonMapper.getChildren(inMap);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		
		rtnString = (String)inMap.get("rtnStr");
		
		return rtnString;
		
	}
	
	public List<ZTreeInfo> getOrgInfo(List<Integer> param)
	{
		List<ZTreeInfo> orgInfos = null;
		
		SqlSession session = null;
		
		try 
		{
			session = MyBatisSessionFactory.getSession();			
			CommonMapper commonMapper = session.getMapper(CommonMapper.class);
			orgInfos = commonMapper.getOrgInfo(param);
		}
		catch (Exception e) 
		{
			e.printStackTrace();
		}
		finally
		{
			session.close();
		}
		
		return orgInfos;
	}

	public void addOrganiztion(Organization organization) throws Exception
	{
		orgDao.addOrganization(organization);
		
	}

	public void updateOrganiztion(Organization organization) throws Exception
	{
		orgDao.updateOrganization(organization);
		
	}

	public void delOrganiztion(int orgId) throws Exception
	{
		orgDao.delOrganization(orgId);
		
	}

	public Organization getOrganization(int orgId) 
	{		
		return orgDao.getOrganization(orgId);
	}

	
	
}
